package logic;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;

import logic.strategy.EliminateStrategy;
import logic.strategy.RandomStrategy;
import logic.strategy.Strategy;

public class MissionEliminatePlayer implements Mission, Serializable {

	private static final long serialVersionUID = 1L;
	private String mission;
	private Player playerToEliminate;

	public MissionEliminatePlayer(Player player) {
		this.playerToEliminate = player;
		this.mission = "need to eliminiate " + player.getPlayerName();
	}
	
	public String getMission() {
		return this.mission;
	}
	
	public String toString() {
		return this.getMission();
	}
	
	public boolean isMissionAccomplished(Player player) {
		return (playerToEliminate.isDefeated() && !player.isDefeated());
	}
	
	public boolean isLegal(Player player){
		return !(player.equals(playerToEliminate));
	}
	
	public boolean saveMission(BufferedWriter bwriter) {
		try {
			bwriter.write("eliminate:" + this.playerToEliminate.getPlayerName());
		} catch (IOException e) {
			System.err.println("An error occured while saving, please try again later.");
			return false;
		}
		return true;
	}
	/**
	 * Returns the Eliminate Player Strategy according to
	 * the Eliminate Player mission
	 */
	public Strategy getStrategy() {
		return new EliminateStrategy(playerToEliminate);
		//return new RandomStrategy();
	}

	/**
	 * return true iff the mission anf this are equal
	 */
	public boolean equals(Mission mission) {
		if(!(mission instanceof MissionEliminatePlayer)) return false;
		MissionEliminatePlayer temp = (MissionEliminatePlayer)mission;
		if(temp.playerToEliminate.equals(this.playerToEliminate)) 
			return true;
		else
			return false;
	}

}
